package svgundoredo;

/*
 * The grouping command class
 */
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;

public class GroupElementCommand extends AbstractCommand
{
    private SVGGroup group;
    private SVGElement elems[];
    
    /**
     * This contructor take the selected element and the new group element
     * 
     * @param elems
     *            selected element to be group
     * @param group
     *            should be the new group
     */
    public GroupElementCommand(SVGElement elems[], SVGGroup group)
    {
        this.elems = elems;
        this.group = group;
    }
    
    @Override
    public void execute()
    {
        // TODO Auto-generated method stub
        for (SVGElement elem : elems)
        {
            elem.getParent().removeChild(elem);
            group.appendChild(elem);
        }
    }
    
    @Override
    public void undo()
    {
        SVGElement[] children = group.getChildren();
        for (SVGElement child : children)
        {
            child.getParent().removeChild(child);
            group.getDocument().getRootGroup().appendChild(child);
        }
        
    }
    
}
